package com.lipuwater.agent.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.lipuwater.agent.R;


public class ProgressWebView extends RelativeLayout {

    private Context context;

    private WebView mWebView = null; //
    private ProgressBar progressBar = null; // 水平进度条
    private int barHeight = 8; // 水平进度条的高
    private boolean isAdd = false; // 判断是否已经加入进度条

    public ProgressWebView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context.getApplicationContext();
        init();
    }

    public ProgressWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context.getApplicationContext();
        init();
    }

    public ProgressWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        this.context = context.getApplicationContext();
        init();
    }

    private void init() {
        mWebView = new WebView(context);
        WebSettings settings = mWebView.getSettings();
        settings.setSupportZoom(false);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        this.addView(mWebView, LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        mWebView.setWebViewClient(new WebViewClient() {//防止页面在浏览器中打开
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    progressBar.setVisibility(View.GONE);
                } else {
                    if (!isAdd) {
                        progressBar = (ProgressBar) LayoutInflater.from(context).inflate(
                                R.layout.webview_progress_horizontal, null);
                        progressBar.setMax(100);
                        progressBar.setProgress(0);
                        addView(progressBar, LayoutParams.FILL_PARENT, barHeight);
                        isAdd = true;
                    }

                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(newProgress);
                }
            }
        });
    }


    public WebView getWebView() {
        return this.mWebView;
    }

    public void setBarHeight(int height) {
        barHeight = height;
    }

    public void setClickable(boolean value) {
        mWebView.setClickable(value);
    }

    public void setUseWideViewPort(boolean value) {
        mWebView.getSettings().setUseWideViewPort(value);
    }

    public void setSupportZoom(boolean value) {
        mWebView.getSettings().setSupportZoom(value);
    }

    public void goBack() {
        mWebView.goBack();
    }

    public boolean canGoBack() {
        return mWebView.canGoBack();
    }

    public void setBuiltInZoomControls(boolean value) {
        mWebView.getSettings().setBuiltInZoomControls(value);
    }

    public void setJavaScriptEnabled(boolean value) {
        mWebView.getSettings().setJavaScriptEnabled(value);
    }

    public void setCacheMode(int value) {
        mWebView.getSettings().setCacheMode(value);
    }

    public void setWebViewClient(WebViewClient value) {
        mWebView.setWebViewClient(value);
    }

    public void loadUrl(String url) {
        mWebView.loadUrl(url);
    }

    public WebSettings getSettings() {
        return mWebView.getSettings();
    }

}
